<?php
/**
 * Created by PhpStorm.
 * User: MrHung
 * Date: 5/16/14
 * Time: 4:14 PM
 */

namespace Frontend\Controller;


use Zend\Mvc\Controller\AbstractActionController;
use Velacolib\Utility\Utility;


class SearchController extends AbstractActionController
{

    protected $topicModel;
    protected $topiccatModel;

    public function onDispatch(\Zend\Mvc\MvcEvent $e)
    {
        //Utility::CheckLogin($this,'/login');
        $this->topicModel = Utility::UserGateway('Admin\Model\AdminTopicTable');
        $this->topiccatModel = Utility::UserGateway('Admin\Model\AdminCategoriesTable');

        $this->layout('frontend/layout');
        return parent::onDispatch($e);
    }

    public function indexAction()
    {
        $mobile_detect = new MobileDetect;
        $isMobile = $mobile_detect->isMobile();
        $isTablet = $mobile_detect->isTablet();
        $item_per_page = $this->params()->fromQuery('ipg');
        if ($item_per_page == 0) $item_per_page = 50;
        $data = $this->topicModel->fetch();
        $data->setCurrentPageNumber((int)$this->params()->fromQuery('page', 1));
        $data->setItemCountPerPage($item_per_page);
        $data->setPageRange(5);

        $search_val = $this->params()->fromRoute('search_val');
        $search_cat = $this->params()->fromRoute('search_cat');
        $search_type = $this->params()->fromRoute('search_type');
        $search_loc = $this->params()->fromRoute('search_loc');
        $cat = $this->topiccatModel->fetchAll(true);
        $cat->setItemCountPerPage(1000);

        $data = $this->topicModel->searchFullText($search_val, $search_cat, $search_type, $search_loc);

        $template_path = 'frontend/index/index_list.phtml';

        $count_result = count($data);
        ($count_result > 0) ? $result = true : $result = false;
        $return['title'] = DEFAULT_DESCRIPTION;
        $return['data'] = $data;
        $return['cat'] = $cat;
        $return['ipg'] = $item_per_page;
        $return['result'] = $result;
        $view = new ViewModel($return);
        $view->setTemplate($template_path);
        return $view;
    }

} 